package com;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "VideoServlet")
public class VideoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");//设置成返回html页面
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        String devid=request.getParameter("devid");
        System.out.println("本次设备"+devid+"请求播放");
        writer.print(
                "<html>\n" +
                "    <head>\n" +
                "        <title>设备视频</title>\n" +
                "        <link href=\"lib/css/video-js.css\" rel=\"stylesheet\">\n" +
                "        <!--引入播放器js-->\n" +
                "        <script src=\"lib/js/video.js\"></script>\n" +
                "        <script src=\"lib/js/videojs-flash.js\"></script>\n" +
                "        <script src=\"lib/js/jquery-3.4.1.js\" type=\"text/javascript\"></script>\n" +
                "        <script type=\"text/javascript\">\n" +
                "            // 设置flash路径,用于在videojs发现浏览器不支持HTML5播放器的时候自动唤起flash播放器\n" +
                "            videojs.options.flash.swf = 'https://cdn.bootcss.com/videojs-swf/5.4.1/video-js.swf';\n" +
                "            let player=videojs(\"my-player\"); //my-player为页面video元素的id\n" +
                "            player.play(); //播放\n" +
                "        </script>" +
                "    </head>\n" +
                "    <body>\n" +
                "    <a href=\"video.jsp\" style=\"width: 150px\">重新选择观看的设备</a>"+
                "    <video id=\"my-player\" class=\"video-js vjs-default-skin vjs-big-play-centered\" preload=\"auto\" autoplay=\"autoplay\"\n" +
                "           data-setup='{}' style=\"position: absolute;margin-left: 0;margin-top: 0;width: 100%;height: 100%\">\n" +
                "        <!--src: 规定媒体文件的 URL  type:规定媒体资源的类型-->\n" +
                "        <source id=\"rtmp\" src=\"rtmp://39.105.13.243:18890/live/"+devid+"\" type=\"rtmp/flv\"/>\n" +
                "    </video>\n" +
                "    </body>\n" +
                "</html>");
        writer.flush();
        writer.close();
    }

    /**
     *  获取设备识别号
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        ObjectMapper om=new ObjectMapper();
        List<String> list=new ArrayList<>();
        list.add("请选择设备");
        list.add("home");
        list.add("home1");
        String json=om.writeValueAsString(list);
        PrintWriter writer = response.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
    }
}
